from typing import Optional, List, Dict, Any
from enum import Enum
from pydantic import BaseModel, Field, validator

class FileType(str, Enum):
    """文件类型枚举"""
    IMAGE = "image"
    DOCUMENT = "document"
    VIDEO = "video"
    AUDIO = "audio"

class FileUploadRequest(BaseModel):
    """文件上传请求模型"""
    user: str = Field(..., description="用户ID")
    file_name: Optional[str] = Field(None, description="文件名称")
    
    @validator('user')
    def user_not_empty(cls, v):
        """验证用户ID不为空"""
        if not v or not v.strip():
            raise ValueError("用户ID不能为空")
        return v

class FileUploadResponse(BaseModel):
    """文件上传响应模型"""
    id: str = Field(..., description="文件ID")
    name: str = Field(..., description="文件名")
    size: int = Field(..., description="文件大小（字节）")
    extension: str = Field(..., description="文件扩展名")
    mime_type: str = Field(..., description="MIME类型")
    created_by: str = Field(..., description="上传人ID")
    created_at: int = Field(..., description="创建时间戳")
    url: Optional[str] = Field(None, description="文件访问URL，非官方API字段，用于内部使用")
    
    class Config:
        """Pydantic配置"""
        arbitrary_types_allowed = True

class FileListResponse(BaseModel):
    """文件列表响应模型"""
    data: List[FileUploadResponse] = Field(..., description="文件列表")
    has_more: bool = Field(..., description="是否有更多文件")
    limit: int = Field(..., description="每页数量限制")
    offset: int = Field(..., description="偏移量")
    total: int = Field(..., description="总文件数")

class FileDeleteRequest(BaseModel):
    """文件删除请求模型"""
    user: str = Field(..., description="用户ID")

class FileDeleteResponse(BaseModel):
    """文件删除响应模型"""
    result: str = Field(..., description="操作结果")

class FileErrorResponse(BaseModel):
    """文件操作错误响应模型"""
    error: str = Field(..., description="错误消息")
    error_code: str = Field(..., description="错误代码")
    status: str = Field("error", description="状态")
    
    class Config:
        """Pydantic配置"""
        json_schema_extra = {
            "example": {
                "error": "文件上传失败",
                "error_code": "file_upload_error",
                "status": "error"
            }
        }

class FileQueryParams(BaseModel):
    """文件查询参数模型"""
    user: str = Field(..., description="用户ID")
    limit: Optional[int] = Field(20, description="每页数量限制，默认20")
    offset: Optional[int] = Field(0, description="偏移量，默认0")
    
    @validator('limit')
    def limit_range(cls, v):
        """验证限制范围"""
        if v < 1 or v > 100:
            return 20  # 如果超出范围，默认为20
        return v
    
    @validator('offset')
    def offset_not_negative(cls, v):
        """验证偏移量非负"""
        if v < 0:
            return 0
        return v
